import errno

import jwt
from django.shortcuts import render

# Create your views here.
from rest_framework import response, status
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.index.models import IndexUser
from apps.list.models import ListInfo
from apps.list.serializers import ListInfoSerializer


class ListView(APIView):
    def get(self,request):
        list_info = ListInfo.objects.all()
        serializer = ListInfoSerializer(list_info,many=True)
        print(list_info+'--------------------------')
        return Response(serializer.data)

    def post(self,request):
        name = request.data.get('name')
        job = request.data.get('job')
        age = request.data.get('age')
        sex = request.data.get('sex')
        have = request.data.get('have')
        play = request.data.get('play')

        token = self.request.META.get('HTTP_TOKEN')
        print(token)
        if not token:
            return Response({'errno': 2, 'errmsg': '未登录'})
        user = jwt.decode(token, verify=False)

        print(user['user_id'])
        user_id = IndexUser.objects.get(id=user['user_id'])
        print(user_id)
        data_info = ListInfo.objects.create(
            user=user_id,
            name=name,
            job=job,
            age=age,
            sex=sex,
            have=have,
            play=play
        )
        serializer = ListInfoSerializer(data_info)

        data = {
            "data":serializer.data
        }

        return Response(data,status=status.HTTP_201_CREATED)
        # serializer = ListInfoSerializer(data=request.data)
        # if serializer.is_valid():
        #     serializer.save()
        #     return Response(serializer.data, status=status.HTTP_201_CREATED)
        # else:
        #     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        # print(request.data)
        #
        # name = request.data.get('name')
        # # print(name)
        # job = '学生'
        # age = request.data.get('age')
        # sex = request.data.get('sex')
        # have = request.data.get('have')
        # play = request.data.get('play')
        # user_id = 1
        # data = ListInfo.objects.create(
        #    name=name,
        #     job=job,
        #     age=age,
        #     sex=sex,
        #     have=have,
        #     play=play,
        #     user_id=user_id
        # )
        # data = {
        #     "errno":0,
        #     "errmsg":0,
        #     "data":{}
        # }
        # return Response(data=data,status=status.HTTP_201_CREATED)
        # else:
            # return Response(serializer.data,status=status.HTTP_400_BAD_REQUEST)



















